
#include "gps_parser.h"

#include <fstream>
#include "boost/scoped_ptr.hpp"
#include "glog/logging.h"
#include "attributes.h"
#include "date_time.h"
#include "expat_parser.h"
#include "gps.pb.h"

namespace gps_proto {

GpsParser::GpsParser(GpsProto* gps_proto)
  : gps_proto_(gps_proto),
    track_proto_(NULL),
    segment_proto_(NULL),
    point_proto_(NULL) {
}

void GpsParser::GatherCharData() {
  gather_char_data_ = true;
  char_data_.clear();
}

const std::string& GpsParser::GetCharData() const {
  return char_data_;
}

// virtual
void GpsParser::CharData(const XML_Char* str, int len) {
  if (gather_char_data_) {
    char_data_.append(str, len);
  }
}

// static
bool GpsParser::ParseLatLonAttrs(const char** atts, PointProto* point_proto) {
  boost::scoped_ptr<Attributes> attributes(Attributes::Create(atts));
  if (!attributes.get()) {
    return false;
  }
  if (point_proto) {
    double lat;
    double lon;
    if (attributes->GetDouble("lat", &lat)) {
      point_proto->set_lat(lat);
    }
    if (attributes->GetDouble("lon", &lon)) {
      point_proto->set_lon(lon);
    }
  }
  return true;
}

}  // end namespace gps_proto

